<?php

/*
 * This class is used for as Model for Department table 
 */

class Model_Departmentcontractor extends Model_Base {

    protected $_name = 'tbl_con_department';
    protected $_primaryKey = 'con_dep_id';

   
  
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('con' => 'tbl_con_department'))
                ->setIntegrityCheck(false)
                ->where('con_dep_id = ?', $id)
                ->where('con_dep_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    
    public function fetchDepartmentByContractor($id) {

        $finlaArray = Array();
        $select = $this->select()
                ->from(array('que' => 'tbl_con_department'))
                ->where('cont_id = ?', $id)
                ->where('con_dep_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $resultData = $data->toArray();
            foreach ($resultData as $key => $_value) {
                    $finlaArray[$_value['con_dep_id']] = $_value['dep_id'];
            }
            return $finlaArray;
        } else {
            return $finlaArray;
        }
    }
    /**
     *  This function is used to get departments
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchDepartmentcontractor($where = "", $limit = "", $offset = "0", $order = 'con_dep_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('con' => 'tbl_con_department'))
                ->setIntegrityCheck(false)
                ->where('con_dep_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getDepartmentcontractorCount($where) {
        $select = $this->select()
                ->from(array('con' => 'tbl_con_department'), Array('cnt' => 'count(*)'))
                ->where('con_dep_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete departments
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteDepartmentcontractor($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_department) {
                $departmentData = Array();
                $departmentData = $this->fetchEntryById($_department);
                if (count($departmentData) > 0) {
                    $data = array('con_dep_status' => '2');
                    $this->update($data, 'con_dep_id =' . (int) $_department);
                }
            }
        } else {
            $dep_id = $prdtArray;
            $departmentData = $this->fetchEntryById($dep_id);
            if (count($departmentData) > 0) {

                $data = array('con_dep_status' => '2');
                $this->update($data, 'con_dep_id =' . $dep_id);
            }
        }
    }

    
    
     public function fetchSITEByContractor($id) {

        $finlaArray = Array();
        $select = $this->select()
                ->from(array('que' => 'tbl_con_department'))
                ->setIntegrityCheck(false)
                ->join(array('cmp' => 'tbl_department'), "cmp.dep_id = que.dep_id")
                ->where('cont_id = ?', $id)
                ->where('con_dep_status = 1');
        // see reasoning in fetchEntries() as to why we return only an array
        $data = parent::fetchAll($select);
        
       # echo $select;exit;
        if (!empty($data)) {
            $resultData = $data->toArray();
            foreach ($resultData as $key => $_value) {
               //  $finlaArray[$_value['con_dep_id']] = $_value['dep_id'];
                 $finlaArray[$_value['site_id']] = $_value['site_id'];
            }
            return $finlaArray;
        } else {
            return $finlaArray;
        }
    }
}
